/* eslint-disable no-undef */
const { app, autoUpdater, BrowserWindow, screen } = require('electron')
// include the Node.js 'path' module at the top of your file
const path = require('path')

// 禁用自动更新
autoUpdater.autoUpdater = false
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = false

// 是否是生产环境
const isPackaged = app.isPackaged

const createWindow = () => {
  const win = new BrowserWindow({
    // 设置窗口尺寸为屏幕工作区尺寸
    width: screen.getPrimaryDisplay().bounds.width,
    height: screen.getPrimaryDisplay().bounds.height,
    // 设置最小尺寸
    minWidth: 800,
    minHeight: 600,

    webPreferences: { nodeIntegration: true, preload: path.join(__dirname, 'preload.cjs') }
  })

  //窗口全屏
  win.setMenuBarVisibility(false)
  win.maximize()
  // win.show()

  // 开发环境下，打开开发者工具。
  if (!isPackaged) {
    win.webContents.openDevTools()
  }

  win.loadURL('http://localhost:3004/')
  // win.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`)
}

//等待app加载完成在打开窗口
app.whenReady().then(() => {
  createWindow()

  //macOS如果没有窗口就打开一个新的窗口
  // app.on("activate", () => {
  //   if (BrowserWindow.getAllWindows().length === 0) createWindow();
  // });
})

//窗口关闭事件(Windows和Linux;直接关闭)
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})
